package com.xxq.aidlclient.Activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xxq.aidlclient.R;
import java.util.List;

import com.xxq.aidlserver.NewsInfo;

/**
 * ListView 的适配器 NewsAdapter.
 */
public class NewsAdapter extends BaseAdapter {

    private int resourceId;
    private Context context;
    private List<NewsInfo> mNewsList;

    public NewsAdapter(Context context, int textViewResourceId, List<NewsInfo> mNewsList) {
        this.context = context;
        resourceId = textViewResourceId;
        this.mNewsList = mNewsList;
    }
    @Override
    public int getCount() {
        return mNewsList.size();
    }
    @Override
    public Object getItem(int position) {
        return mNewsList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        View view;
        NewsInfo news = (NewsInfo) getItem(position);

        if (convertView == null) {
            view= LayoutInflater.from(context).inflate(resourceId, null);

            viewHolder = new ViewHolder();

            //获取view上的子控件对象
            viewHolder.newsTitle = (TextView) view.findViewById(R.id.newsTitle);
            viewHolder.newsUrl = (TextView) view.findViewById(R.id.newsUrl);
            viewHolder.newsTime = (TextView) view.findViewById(R.id.newsTime);

            view.setTag(viewHolder);

        } else {
            view=convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        //将数据设置给这些子控件做显示
        viewHolder.newsTitle.setText(news.getNewsTitle());
        viewHolder.newsUrl.setText(news.getNewsUrl());
        viewHolder.newsTime.setText(news.getNewsTime());

        return view;
    }

    static class ViewHolder {
        public TextView newsTitle;
        public TextView newsUrl;
        public TextView newsTime;
    }
}
